#include "StdAfx.h"
#include "TGSvrNetMan.h"
#include "TG_NET_TGA_DEF.h"
#include "stdio.h"


int TGSvrNetMan::TcpAnchorDispatch(SOCKET hClientSocket,NET_PACKET_HEAD *pMsgHead,BYTE *pBuffer)
{
	int nRet = TG_ERR_SUCCESS;

	char szRetBuffer[256];
	memset(szRetBuffer,0,sizeof(szRetBuffer));

	NET_PACKET_HEAD *pRetMsgHead = (NET_PACKET_HEAD*)szRetBuffer;
	pRetMsgHead->nMsgType = pMsgHead->nMsgType + 1;

	SOCKADDR_IN addrPeer;
	int nAddrLen = sizeof(addrPeer);
	getpeername(hClientSocket, (PSOCKADDR) &addrPeer, &nAddrLen);

	char szPeerIP[128] = { 0 };
	sprintf(szPeerIP, "%s", inet_ntoa(addrPeer.sin_addr));

#ifdef dkq_test_svrnet
	//((TGSvrNetMan*)this)->m_xLog.WriteLog(__FILE__,__LINE__,"in nMsgType:%d",pMsgHead->nMsgType);
#endif

	switch( pMsgHead->nMsgType )
	{
	case MSG_TYPE_ANC_LOGIN:
		{
			if (pMsgHead->nDataLen   != sizeof(TG_ANCHOR_INFO))
			{
				nRet = TG_ERR_NETWORK_SEND_ERROR ;
				break;
			}

			SvrNetRPSession*  pSession = m_xSessionMan.AddSession(this,(TG_ANCHOR_INFO*)pBuffer);
			if ( pSession == NULL )
			{
				nRet = TG_ERR_FAILURE ;
				break ;
			}
			else
			{
				nRet = pSession->StartRealStream(hClientSocket,TG_NET_PROTOCOL_TCP);
			}
		}
		break;
	case MSG_TYPE_REALSTREAM:
		{
			nRet = TG_ERR_NETWORK_FAIL_CONNECT;	
		}
		break;
	case MSG_TYPE_TALK:
		{

		}
		break;
	case MSG_TYPE_BROADCAST:
		{
		}
		break;
	case MSG_TYPE_FILE:
		{
		}
		break;
	default:
		{
			nRet = TG_ERR_FAILURE;
		}
		break;
	}

#ifdef dkq_test_svrnet
	//((TGSvrNetMan*)this)->m_xLog.WriteLog(__FILE__,__LINE__,"out nMsgType:%d",pMsgHead->nMsgType);
#endif

	pRetMsgHead->nFrameSize = 0;
	pRetMsgHead->nErrFlag   = nRet;
	pRetMsgHead->nDataLen = 0 ;
	pRetMsgHead->SetCRC();

	DWORD dwRet = 0;
	TGSendDataS(hClientSocket,
		(char*)szRetBuffer,sizeof(NET_PACKET_HEAD),
		&dwRet,
		m_xAnchorListen.m_hWSASREvent,
		m_xAnchorListen.m_hWSAExitEvent,TG_WAITTIME);
	
	if(nRet != TG_ERR_SUCCESS)
	{
		TGShutDownSocket( hClientSocket );
		TGCloseSocket( hClientSocket );
		hClientSocket = NULL;
	}
	return nRet;
}

int TGSvrNetMan::UdpAnchorDispatch(SOCKET hSocket,TG_UDP_ADDR *pAddr,NET_PACKET_HEAD *pMsgHead,BYTE *pBuffer)
{
	int nRet = TG_ERR_SUCCESS;

	char szRetBuffer[256];
	memset(szRetBuffer,0,sizeof(szRetBuffer));

	NET_PACKET_HEAD *pRetMsgHead = (NET_PACKET_HEAD*)szRetBuffer;
	pRetMsgHead->nMsgType = pMsgHead->nMsgType + 1;

	char szPeerIP[128] = { 0 };
	sprintf(szPeerIP, "%s", inet_ntoa(pAddr->xAddr.sin_addr));

	switch( pMsgHead->nMsgType )
	{
	case MSG_TYPE_ANC_LOGIN:
		{
		}
		break;
	case MSG_TYPE_TALK:
		{
		}
		break;
	case MSG_TYPE_BROADCAST:
		{
		}
		break;
	case MSG_TYPE_FILE:
		{
		}
		break;
	default:
		{
			nRet = UdpAnchorCommand(hSocket,pAddr,pMsgHead,pBuffer);
		}
		break;
	}

	if( TG_ERR_SUCCESS != nRet )
	{
		pRetMsgHead->nFrameSize = 0;
		pRetMsgHead->nErrFlag   = nRet;

		sendto( hSocket,
			(char*)szRetBuffer,sizeof(NET_PACKET_HEAD),
			0,
			(struct sockaddr*)&pAddr->xAddr,pAddr->nAddrLen );
	}

	return nRet;
}

int TGSvrNetMan::UdpAnchorCommand(SOCKET hSocket,TG_UDP_ADDR *pAddr,NET_PACKET_HEAD *pMsgHead,BYTE *pBuffer)
{
	int nRet = TG_ERR_SUCCESS;

	switch( pMsgHead->nMsgType )
	{
	case 1:
		{

		}
		break;
	default:
		{
			nRet = TG_ERR_FAILURE;
			break;
		}
	}

	return nRet;
}